﻿/*
 * @author: wizardc
 */

using UnityEngine;
using UnityEngine.UI;

namespace Dou.Extend
{
    /// <summary>
    /// RectTransform 方法扩展
    /// </summary>
    public static class RectTransformExtend
    {
        public static void SetActive(this RectTransform rectTransform, bool active)
        {
            rectTransform.localScale = active ? Vector3.one : Vector3.zero;
        }
        
        public static void SetAnchorMin(this RectTransform transform, float x, float y)
        {
            transform.anchorMin = new Vector2(x, y);
        }

        public static void GetAnchorMin(this RectTransform transform, out float x, out float y)
        {
            var anchorMin = transform.anchorMin;
            x = anchorMin.x;
            y = anchorMin.y;
        }
        
        public static void SetAnchorMax(this RectTransform transform, float x, float y)
        {
            transform.anchorMax = new Vector2(x, y);
        }

        public static void GetAnchorMax(this RectTransform transform, out float x, out float y)
        {
            var anchorMax = transform.anchorMax;
            x = anchorMax.x;
            y = anchorMax.y;
        }

        public static void SetPivot(this RectTransform transform, float x, float y)
        {
            transform.pivot = new Vector2(x, y);
        }

        public static void GetPivot(this RectTransform transform, out float x, out float y)
        {
            var pivot = transform.pivot;
            x = pivot.x;
            y = pivot.y;
        }
        
        public static void SetAnchoredPosition(this RectTransform transform, float x, float y)
        {
            transform.anchoredPosition = new Vector2(x, y);
        }
        
        public static void GetAnchoredPosition(this RectTransform transform, out float x, out float y)
        {
            var anchoredPosition = transform.anchoredPosition;
            x = anchoredPosition.x;
            y = anchoredPosition.y;
        }

        public static void SetSizeDelta(this RectTransform transform, float width, float height)
        {
            transform.sizeDelta = new Vector2(width, height);
        }

        public static void GetSizeDelta(this RectTransform transform, out float width, out float height)
        {
            var sizeDelta = transform.sizeDelta;
            width = sizeDelta.x;
            height = sizeDelta.y;
        }

        public static Vector2 GetRectSize(this RectTransform transform)
        {
            var rectSize = new Vector2();
            if (transform.anchorMin.x != transform.anchorMax.x)
            {
                var rect = transform.rect;
                rectSize.x = rect.size.x;
            }
            else
            {
                rectSize.x = transform.sizeDelta.x;
            }
            if (transform.anchorMin.y != transform.anchorMax.y)
            {
                var rect = transform.rect;
                rectSize.y = rect.size.y;
            }
            else
            {
                rectSize.y = transform.sizeDelta.y;
            }
            return rectSize;
        }

        public static bool RectangleContainsScreenPoint(this RectTransform transform, Vector2 screenPoint, Camera camera)
        {
            return RectTransformUtility.RectangleContainsScreenPoint(transform, screenPoint, camera);
        }

        public static bool RectangleContainsScreenPoint(this RectTransform transform, Vector2 screenPoint)
        {
            return RectTransformUtility.RectangleContainsScreenPoint(transform, screenPoint);
        }

        public static bool RectangleContainsScreenPoint(this RectTransform transform, float screenX, float screenY)
        {
            return RectTransformUtility.RectangleContainsScreenPoint(transform, new Vector2(screenX, screenY));
        }

        public static bool ScreenPointToLocalPointInRectangle(this RectTransform transform, Vector2 screenPoint, Camera camera, out Vector2 localPoint)
        {
            return RectTransformUtility.ScreenPointToLocalPointInRectangle(transform, screenPoint, camera, out localPoint);
        }

        public static bool ScreenPointToLocalPointInRectangle(this RectTransform transform, Vector2 screenPoint, out Vector2 localPoint)
        {
            Camera camera = null;
            var canvas = transform.GetComponentInParent<Canvas>();
            if (canvas != null)
            {
                camera = canvas.worldCamera;
            }
            return RectTransformUtility.ScreenPointToLocalPointInRectangle(transform, screenPoint, camera, out localPoint);
        }

        public static Vector3 LocalPointInOtherRectangle(this RectTransform transform, RectTransform otherTransform, Vector3 localPoint)
        {
            if (otherTransform != null)
            {
                return transform.localToWorldMatrix * otherTransform.worldToLocalMatrix * localPoint;
            }
            return default;
        }

        public static void ForceRebuildLayoutImmediate(this RectTransform transform)
        {
            LayoutRebuilder.ForceRebuildLayoutImmediate(transform);
        }

        public static void MarkLayoutForRebuild(this RectTransform transform)
        {
            LayoutRebuilder.MarkLayoutForRebuild(transform);
        }
    }
}
